package obj;

import data.DataHelper;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Charger {

    public static final String CHARGERS_PATH_NETWORK = "data/chargers/charger.%s.orig.txt";
    public static final String CHARGERS_PATH_AUTO = "data/chargers/car.charger.%s.orig.txt";
    public static final String ORIG_URL_NETWORK = "http://extramobi.ru/product/setevoe_zaryadnoe_ustroystvo_";
    public static final String ORIG_URL_AUTO = "http://extramobi.ru/product/avtomobilnoe_zaryadnoe_ustroystvo_";

    public static final String AAA_URL_NETWORK = "http://extramobi.ru/product/%s_network_chargers/";
    public static final String AAA_URL_AUTO = "http://extramobi.ru/product/%s_auto_chargers/";

    public String name;
    public String brandName;
    public String model;
    public int price;
    public int oldPrice;
    public int count;
    public String[] models;
    public List<String> modelURLs;
    public String url;
    public boolean auto;
    public String typeUrl = "setevoe";
    public String type = "Сетевое";
    public String prefix = "СЗУ";

    public static Charger parseCharger(String line, double coef, boolean auto) {
        Charger charger = new Charger();
        String[] fields = line.split(";");
        charger.name = fields[0];
        charger.models = fields[1].split(",");
        charger.count = Integer.valueOf(fields[5].trim());
        charger.price = Integer.valueOf(fields[4]);
        charger.oldPrice = (int) (charger.price * coef);
        String brand = fields[0].split("\\s")[0];
        charger.auto = auto;
        if (brand.equals("Sony")) {
            brand = "Sony Ericsson";
        }
        charger.brandName = brand;
        charger.model = fields[3];
        if (charger.auto) {
            charger.url = DataHelper.codeInURL(ORIG_URL_AUTO + charger.name).toLowerCase();
            charger.type = "Автомобильное";
            charger.typeUrl = "avtomobilnoe";
            charger.prefix = "АЗУ";
        } else {
            charger.url = DataHelper.codeInURL(ORIG_URL_NETWORK + charger.name).toLowerCase();
        }
        charger.modelURLs = new ArrayList<String>();
        for (String model : charger.models) {
            String modelUrl;
            if (charger.auto) {
                modelUrl = DataHelper.codeInURL(ORIG_URL_AUTO + charger.brandName + "_" + model);
            } else {
                modelUrl = DataHelper.codeInURL(ORIG_URL_NETWORK + charger.brandName + "_" + model);
            }
            charger.modelURLs.add(modelUrl);
        }
        return charger;
    }

    public static List<Charger> parseChargers(String brandName, double coef) throws IOException {
        List<Charger> chargers = new ArrayList<Charger>();
        chargers.addAll(parseChargers(brandName, coef, true));
        chargers.addAll(parseChargers(brandName, coef, false));
        return chargers;
    }

    public static List<Charger> parseChargers(String brandName, double coef, boolean auto) throws IOException {
        String path = auto ? CHARGERS_PATH_AUTO : CHARGERS_PATH_NETWORK;
        BufferedReader reader = new BufferedReader(new FileReader(String.format(path, brandName.replace("Sony Ericsson", "se"))));
        List<Charger> chargers = new ArrayList<Charger>();
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.contains("﻿")) {
                line = line.replace("﻿", "");
            }
            Charger charger = Charger.parseCharger(line, coef, auto);
            chargers.add(charger);
        }
        return chargers;
    }

    public static int getPrice(String brand, String model, boolean auto) throws IOException {
        List<Charger> chargers = parseChargers(brand, 1, auto);
        for (Charger charger : chargers) {
            for (String chModel : charger.models) {
                if (model.equalsIgnoreCase(chModel)) {
                    return charger.price;
                }
            }
        }
        throw new IllegalArgumentException("Unknown model :" + brand + " " + model);
    }
}
